//src/api/common.js
import request from '@/utils/request'

/**
 * POST 方法 与 GET方法  区别
 * post==> data: obj
 * get==>  params: obj
 */
let headers={
    'Content-Type':'application/json;charset=UTF-8',
    'Accept':'application/jsons',
    'Request-From':'mobile'
}
let token = sessionStorage.getItem('loginToken') || null
if (token) {
    // 如果token不为null，否则传token给后台
    headers.Token = token
}
const post  = function (url,obj){
  return request({
      url: url,
      method: 'POST',
      data: obj,    //----------------->>>区别
      headers:headers
    })
}
const get  = function (url,obj){
  return request({
      url: url,
      method: 'GET',
      params: obj,    //----------------->>>区别
      headers:headers
  })
}
const file = function (url,obj){
    let file = new window.File([obj], '一张图片', {type: obj})
    // console.log(file)
    let fd = new FormData()
    fd.append('img', file)
    //上传图片
    headers['Content-Type']='multipart/form-data'
    return request({
        url: url,      // url = base url + request url
        method: 'POST',
        data: fd,    //----------------->>>区别
        headers
    })
}
const axios = {
  post,
  get,
  file
}
export default axios
